// pages/addressupdate/addressupdate.js
const app =getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    switch1Checked: true,
    region: ['广东省', '广州市', '海珠区'],

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(JSON.parse(options.updatas))
    const op=JSON.parse(options.updatas)
    var arr=[op.consigneeProvince,op.consigneeCity,op.consigneeArea]

    if (op.defaultFlag==1) {
      
      this.setData({
        switch1Checked:true,
      })
      
    }else{
      this.setData({
        switch1Checked:false,
      })
    }
    this.setData({
      region:arr,
      consigneeName:op.consigneeName,
      consigneeMobile:op.consigneeMobile,
      consigneeAddress:op.consigneeAddress,
      consigneeInfoId:op.consigneeInfoId,
      defaultFlag:op.defaultFlag,

      consigneeProvince:op.consigneeProvince,
      consigneeCity:op.consigneeCity,
      consigneeArea:op.consigneeArea,
    })


  },
  bindKeyname(e){
    // console.log('1')
    this.setData({
      consigneeName:e.detail.value
    })

  },
  bindKeymobile(e){
    // console.log('2')
    this.setData({
      consigneeMobile:e.detail.value
    })
  },
  bindKeyaddress(e){
    // console.log('3')
    this.setData({
      consigneeAddress:e.detail.value
    })
  },
//更改地区
  bindRegionChange: function (e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)

    this.setData({
      consigneeProvince: e.detail.value[0],
      consigneeCity:e.detail.value[1],
      consigneeArea:e.detail.value[2],
      region: e.detail.value
    })
  },
  //选择默认
  switch1Change(){
     this.setData({
      switch1Checked:!this.data.switch1Checked
     })
     if (this.data.switch1Checked) {
      this.setData({
        defaultFlag:1
       })
     }else{
      this.setData({
        defaultFlag:0
       })
     }

  },
  SaveAddress(){
   // consigneeName:op.consigneeName,
     // consigneeMobile:op.consigneeMobile,
      // consigneeAddress:op.consigneeAddress,
    //   consigneeInfoId:op.consigneeInfoId,
    // consigneeProvince:op.consigneeProvince,
    // consigneeCity:op.consigneeCity,
    // consigneeArea:op.consigneeArea,
    // const regu=/^1\d{10}$/;
		// 		if(regu.test(this.phone)&&this.check){
		// 		}else{
		// 			this.utils.toast('手机号格式有误');
		// 		}
    
    var t=this
    var regex=/^1\d{10}$/
    if(t.data.consigneeName==''){
      wx.showToast({
        title: '请输入收货人',
        icon:'none'
      })
    }else if(t.data.consigneeMobile==''){
      wx.showToast({
        title: '请输入联系方式',
        icon:'none'
      })
    }
    else if( !regex.test( t.data.consigneeMobile)){
      wx.showToast({
        title: '请输入正确的联系方式',
        icon:'none'
      })
    }else if(t.data.consigneeProvince==''|| t.data.consigneeCity=='' || t.data.consigneeArea==''){
      wx.showToast({
        title: '请选择所在区',
        icon:'none'
      })
    }else if(t.data.consigneeAddress==''){
      wx.showToast({
        title: '请输入详细地址',
        icon:'none'
      })
    }else{
      wx.request({
        url: app.globalData.Interfaceurl + 'updateConsigneeInfo',
        method: "post",
        header: {
            "content-type": "application/x-www-form-urlencoded;charset=UTF-8"
        },
        data: {
            userId: app.globalData.userId,
            consigneeName:t.data.consigneeName,
            consigneeMobile:t.data.consigneeMobile,
            consigneeProvince:t.data.consigneeProvince,
            consigneeCity:t.data.consigneeCity,
            consigneeArea:t.data.consigneeArea,
            consigneeAddress:t.data.consigneeAddress,
            defaultFlag:t.data.defaultFlag,
            consigneeInfoId:t.data.consigneeInfoId
        },
        success: res => {
            // console.log(res)
            if (res.data.result==0&&res.statusCode==200) {
              var pages = getCurrentPages();
              var prevPage = pages[pages.length - 2];
              prevPage.setData({
                modify:1
              });
              wx.navigateBack({
                delta: 1,
              })
              // wx.redirectTo({
              //   url: '/pages/addressmg/addressmg',
              // })
            }
  
        }
    })
    }

    
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})